package com.yizisu.mvvmlib.demo.welcome

import android.graphics.Color
import android.os.Bundle
import android.view.*
import android.widget.Button

import com.yizisu.basemvvm.logI
import com.yizisu.basemvvm.mvvm.MvvmPopupWindow

import com.yizisu.basemvvm.mvvm.WebViewHelperActivity
import com.yizisu.basemvvm.mvvm.mvvm_helper.createLiveBean
import com.yizisu.basemvvm.mvvm.mvvm_helper.success
import com.yizisu.basemvvm.utils.*
import com.yizisu.basemvvm.utils.websocket.WebSocketHelper
import com.yizisu.basemvvm.utils.websocket.socketio.SocketIo

import com.yizisu.mvvmlib.R
import com.yizisu.mvvmlib.baselib.base.BaseActivity
import com.yizisu.mvvmlib.baselib.helper.MyDialog
import com.yizisu.mvvmlib.baselib.helper.MyPopupWindow
import com.yizisu.mvvmlib.demo.base2Demo.Demo2Activity
import kotlinx.android.synthetic.main.activity_welcome.*
import kotlinx.coroutines.*
import kotlin.coroutines.EmptyCoroutineContext

class WelcomeActivity : BaseActivity() {
    companion object {
        val testLiveData = createLiveBean<String>()
    }

    override fun isNeedToolbar(): Boolean {
        return false
    }

    override fun getContentResOrView(inflater: LayoutInflater): Any? {
        return R.layout.activity_welcome
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
//        toPicActivity.setOnDoubleClickListener()
    }

    override fun initViewModel() {
        testLiveData.registerOnSuccess {
            println("我是第一个界面，收到回调")
        }
    }

    override fun initData() {
        testLiveData.success("6666")
    }

    override fun getClickView(): List<View?>? {
        return listOf(toPicActivity, popupBottom, popupTop, popupButton,
                dialogBottom, dialogTop, dialogCenter, toLoadActivity, webView, downVideo, sendMessageTv,
                downApk, cancelDownApk)
    }

    private val websocket by lazy { WebSocketHelper(SocketIo("http://120.77.183.32:9099/audio?TOKEN=321")) }
    override fun onSingleClick(view: View) {
        super.onSingleClick(view)
        when (view) {
            toPicActivity -> {
                navigateTo(Demo2Activity::class.java)
            }
            popupBottom -> {
                createPopup().showAtBottom()
//                测试动态权限申请
//                PermissionUtil.request(listOf(Manifest.permission.CAMERA,
//                        Manifest.permission.READ_EXTERNAL_STORAGE)) {
//                   logI(it.toString())
//                }
            }
            popupTop -> {
//                createPopup1().showAtTop()
                ViewAnimationUtils.createCircularReveal(root, (view.left + view.right) / 2,
                        (view.top + view.bottom) / 2, 800f,
                        0f).setDuration(5000).start()
            }
            popupButton -> {
                MyPopupWindow(this).showAsDropDown(popupButton)
            }
            dialogBottom -> {
                MyDialog().showAsDropTopOrBottom(view, false)
                createPopup().showAsDropTopOrBottom(view, false)
            }
            dialogTop -> {
                MyDialog().apply {
                    gravity = Gravity.TOP
                }.apply {
                    view.postDelayed({
                        safeShow(this@WelcomeActivity)
                        safeDismiss()
                    }, 5000)
                }
            }
            dialogCenter -> {
                MyDialog().apply {
                    gravity = Gravity.CENTER
                }.show(this)
            }
            toLoadActivity -> {
                MyDialog().showAsDropTopOrBottom(view, false)
                createPopup().showAsDropTopOrBottom(view, false)
            }
            sendMessageTv -> {
                websocket.sendMessage(byteArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9))
            }
            webView -> {
                val title = Button(this)
                WebViewHelperActivity
                        .Builder()
                        .setUrl("https://music.sonimei.cn/")
                        .setStatusBarColor(Color.WHITE)
                        .setStatusBarTextColorIsBlack(true)
                        .setTitleBar(title)
                        .setShowTitleView(title)
                        .setStatusBarIsTransparent(false)
                        .start(this)

            }
            downVideo -> {
//                getPermission(mutableListOf(
//                        Manifest.permission.WRITE_EXTERNAL_STORAGE
//                )) {
//                    if (it) {
                DownloadHelper()
                        .setOverlayFile(false)
                        .start(
                                "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
                                "测试下载视频.mp4",
                                object : DownloadHelper.DownListener() {

                                    override fun onProgressUpdate(downloadSize: Long, totalSize: Long, progress: Long) {
                                        progressBar.progress = progress.toInt()
                                    }

                                    override fun onComplete(result: DownloadHelper.DownloadBean?) {
                                        super.onComplete(result)
                                        logI("下载完成：${result}")
                                        "下载完成".toast()
                                        if (result?.isNewDownloadFile == false) {
                                            "${result.fileName} 已经存在".toast()
                                        }
                                    }

                                    override fun onStart() {
                                        super.onStart()
                                        logI("开始下载")
                                        "开始下载".toast()
                                    }

                                    override fun onError(error: Throwable) {
                                        super.onError(error)
                                        "下载失败".toast()
                                        logI("下载失败：${error.message}")
                                    }
                                }
                        )
            }
            downApk -> {
                UpdateApkHelper.startDown(
                        "https://dc3cc7ee9858505c34ae92df098ec34c.dd.cdntips.com/download.sj.qq.com/upload/connAssitantDownload/upload/MobileAssistant_1.apk?mkey=5e02c737b70e3bff&f=8935&cip=183.14.29.10&proto=https")
            }
            cancelDownApk -> {
                UpdateApkHelper.stopDown()
            }
//                }
//            }
        }
    }

    private fun createPopup(): MvvmPopupWindow {
        return MvvmPopupWindow(Button(this).apply {
            text = "我是PopupWindow"
        })
    }

    private fun createPopup1(): MvvmPopupWindow {
        return MvvmPopupWindow().apply {
            contentView = View.inflate(this@WelcomeActivity, R.layout.popup_window, null)
        }
    }

    override fun isCanSwipeBack(): Boolean {
        return false
    }
}
